package cn.hongyu.service.impl;

import cn.hongyu.dao.RoleDao;
import cn.hongyu.dao.UserDao;
import cn.hongyu.pojo.Role;
import cn.hongyu.pojo.User;
import cn.hongyu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<User> list() {
        List<User> list = userDao.list();
        for(User user:list){
            //取出id值
            Long id = user.getId();
            //将id作为参数查询当前user id对于的roles集合数据 使用roledao
            List<Role> roles = roleDao.fingById(id);
            user.setRoles(roles);
        }
        //封装list中每一个roles数据
        return list;
        }

    @Override
    public void save(User user, Long[] roleIds) {

        Long id = userDao.save(user);
        userDao.saveUserRoleRel(id,roleIds);
    }
}
